import React, { useEffect, useState } from 'react'
import { Button, Result, message } from 'antd'
import qs from 'qs'
import { useNavigate, useLocation } from 'react-router-dom'
import { useApi } from '../../api'
function Pay() {
  const navigate = useNavigate()
  const location = useLocation()
  const { successPay } = useApi()

  const [result, setResult] = useState('')
  const [code, setCode] = useState('')
  const { out_trade_no, trade_no } = qs.parse(location.search)

  useEffect(() => {
    ; (async () => {
      try {
        let res = await successPay(
          qs.stringify({
            out_trade_no,
            trade_no
          })
        )
        if (res.code == 1) {
          message.success(res.message)
        } else {
          message.error(res.message)
        }
        setResult(res.message)
        setCode(res.code)
      } catch (error) {
        message.error('异常')
        console.log('err', error)
      }
    })()
  }, [])

  //跳转页面
  const handlerClick = async type => {
    //首页
    if (type == 0) {
      navigate('/home')
    } else {
      navigate('/personal/homePage')
    }
  }
  return (
    <div className="flex justify-center items-center w-[100vw] h-[100vh] bg-white">
      <Result
        status="success"
        title={code == 1 ? '购买会员成功，欢迎你的加入' : result}
        subTitle={`订单号：${out_trade_no}，请点击确认回到首页`}
        extra={[
          <Button onClick={() => handlerClick(0)} type="primary" key="console">
            确认
          </Button>,
          <Button onClick={() => handlerClick(1)} key="buy">
            个人主页
          </Button>
        ]}
      />
    </div>
  )
}

export default Pay
